/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.NeighborhoodBO;
import org.ipti.br.tag.bo.StreetBO;
import org.ipti.br.tag.bo.ZipcodeBO;
import org.ipti.br.tag.entity.Address;
import org.ipti.br.tag.entity.Zipcode;
import java.util.List;
import javax.inject.Inject;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author luancorumba
 */
@ManagedBean
@RequestScoped
public class AddressMB extends AppBaseBean{

    @Inject
    private NeighborhoodBO neighborhoodBO;
    @Inject
    private StreetBO streetBO;
    @Inject
    private ZipcodeBO zipcodeBO;
    
    public void searchCEP(Address address) {
        address.getNeighborhood().setName("");
        address.getStreet().setName("");
        address.setZipcode(zipcodeBO.searchByCode(address.getZipcode().getCode()));
        if (address.getZipcode() == null) {
            address.setZipcode(new Zipcode());
            addInfoMessage("newHumanF:address:cep", "CEP não cadastrado.", null);
        } else {
            if (address.getZipcode().getNeighborhood() != null) {
                address.setNeighborhood(address.getZipcode().getNeighborhood());
            }
            if (address.getZipcode().getStreet() != null) {
                address.setStreet(address.getZipcode().getStreet()); 
            }
        }
    }

    public List<String> neighborhoodComplete(String query,Address address) {
        return neighborhoodBO.neighborhoodList(query, address.getZipcode().getCity());
    }

    public List<String> streetComplete(String query,Address address) {
        return streetBO.streetList(query, address.getZipcode().getCity());
    }

    public NeighborhoodBO getNeighborhoodBO() {
        return neighborhoodBO;
    }

    public void setNeighborhoodBO(NeighborhoodBO neighborhoodBO) {
        this.neighborhoodBO = neighborhoodBO;
    }

    public StreetBO getStreetBO() {
        return streetBO;
    }

    public void setStreetBO(StreetBO streetBO) {
        this.streetBO = streetBO;
    }

    public ZipcodeBO getZipcodeBO() {
        return zipcodeBO;
    }

    public void setZipcodeBO(ZipcodeBO zipcodeBO) {
        this.zipcodeBO = zipcodeBO;
    }
}
